package com.example.li.service;

import com.example.li.mapper.LawyerMapper;
import com.example.li.model.Lawyer;
import com.example.li.model.User;
import com.example.li.util.ResultDTO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class LawyerService {
    @Autowired
    LawyerMapper lawyerMapper;

    //登录service
    public ResultDTO login(Lawyer lawyer) {
        List<Lawyer> lawyer1 = lawyerMapper.selectByLawLawyerNumber(lawyer);
        if (lawyer1 != null&&lawyer1.size()>0) {
            if (lawyer1.get(0).getLawLawyerPwd().equals(lawyer.getLawLawyerPwd())) {
                return new ResultDTO().success(lawyer1);
            } else {
                return new ResultDTO().failure("密码错误！");
                   }
        } else {
            return new ResultDTO().failure("信息错误！");
        }
    }

    //注册service
    public ResultDTO lawyerRegister(Lawyer lawyer) {
        List<Lawyer> lawyer1 = lawyerMapper.selectByLawLawyerNumber(lawyer);
        if (lawyer1.size()==0) {
            lawyer.setLawAddtime(new Date());
            int i = lawyerMapper.insertSelective(lawyer);
            return new ResultDTO().success();
        } else {
            return new ResultDTO().failure("该用户已经存在!");
        }
    }

    //展示列表
    public List getLawyerList(Lawyer lawyer) {
        //PageHelper.startPage(1, 1);
        List<Lawyer> lawyers = lawyerMapper.selectByLawLawyerNumber(lawyer);
        return lawyers;
    }

    //修改律师信息
    public ResultDTO updateLawyer(Lawyer lawyer) {
        int i = lawyerMapper.updateByPrimaryKeySelective(lawyer);
        return new ResultDTO().success();
    }

    //    删除
    public ResultDTO deleteLawyer(Lawyer lawyer) {
//        Lawyer lawyer1 = lawyerMapper.selectByLawLawyerNumber(lawyer);
        int i = lawyerMapper.deleteByPrimaryKey(lawyer.getId());
        return new ResultDTO().success();
    }
    //通过id找一个用户
    public ResultDTO showLawyer(Lawyer lawyer) {
            Lawyer lawyer1 = lawyerMapper.selectByPrimaryKey(lawyer.getId());
            return new ResultDTO().success(lawyer1);
        }
}